﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 头像抽奖工具
{
    public partial class Form1 : Form
    {
        private Point mousePoint = new Point();
        private List<string> fileList;
        private bool flag;
        private Random rd;

        public Form1()
        {
            InitializeComponent();
            flag = false;
            rd = new Random();
        }

        /// <summary>
        /// 画面加载
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            //获取当前应用程序所在目录
            string path = Application.StartupPath + "\\Images";
            DirectoryInfo directoryInfo = new DirectoryInfo(path);
            fileList = directoryInfo.GetFiles().Where(it => it.FullName.ToLower().EndsWith(".png") || it.FullName.ToLower().EndsWith(".jpg")).Select(it => it.FullName).ToList();
        }

        /// <summary>
        /// 退出按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panel1_Click(object sender, EventArgs e)
        {
            flag = false;
            Thread.Sleep(50);
            Environment.Exit(0);
        }


        /// <summary>
        /// 鼠标按下去 记录位置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            this.mousePoint.X = e.X;
            this.mousePoint.Y = e.Y;
        }


        /// <summary>
        /// 鼠标拖动
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                this.Top = Control.MousePosition.Y - mousePoint.Y;
                this.Left = Control.MousePosition.X - mousePoint.X;
            }
        }


        private void panel3_Click(object sender, EventArgs e)
        {
            if (!flag)
            {
                if (fileList.Count == 0)
                {
                    MessageBox.Show("无头像，请在目录下添加头像后再运行本工具");
                    return;
                }
                flag = !flag;
                panel3.BackgroundImage = Properties.Resources.stop;
                new Thread(StartRun).Start();
            }
            else
            {

                flag = !flag;
                panel3.BackgroundImage = Properties.Resources.start;
                int length = fileList.Count;
                transparentPanel1.fileName = fileList[rd.Next(0, length)];
                transparentPanel1.Invalidate();
            }
        }

        private void StartRun()
        {
            int length = fileList.Count;
            while (flag)
            {
                transparentPanel1.fileName = fileList[rd.Next(0, length)];
                transparentPanel1.Invalidate();
                Thread.Sleep(20);
            }
        }
    }
}
